<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            $table->string('payment_method', 50); // alipay, wechat, mock
            $table->string('transaction_id', 100)->nullable(); // 第三方交易号
            $table->decimal('amount', 10, 2); // 支付金额
            $table->string('status', 20)->default('pending'); // pending, paid, failed, refunded, cancelled
            $table->json('payment_data')->nullable(); // 支付相关数据
            $table->text('failure_reason')->nullable(); // 失败原因
            $table->timestamp('paid_at')->nullable(); // 支付完成时间
            $table->timestamp('expires_at')->nullable(); // 支付过期时间
            $table->timestamps();

            // 索引
            $table->index('order_id');
            $table->index('transaction_id');
            $table->index('status');
            $table->index('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('payments');
    }
};